package com.example.fan.airline_reservation;

import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bin.david.form.annotation.SmartColumn;
import com.bin.david.form.annotation.SmartTable;
import com.bin.david.form.data.style.FontStyle;
import com.daimajia.numberprogressbar.NumberProgressBar;
import com.example.fan.airline_reservation.Model.Fight;
import com.example.fan.airline_reservation.Model.Order;
import com.example.fan.airline_reservation.Model.USER;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SQLQueryListener;

public class Detail extends AppCompatActivity {

    List<PInfo> Plist = new ArrayList<>();
    private com.bin.david.form.core.SmartTable table;
    NumberProgressBar bar;
    LinearLayout load,info;
    TextView sta;

    int allAccount=0;
    int finish=0;
    String flightNum;
    long up,down;
    PInfo p;
    double income=0;

    @SmartTable(name="乘客名单")
    public class PInfo {
        @SmartColumn(id = 1,name = "姓名")
        private String name;
        @SmartColumn(id = 2,name = "性别")
        private String sex;
        @SmartColumn(id = 3,name = "身份证号")
        private String ID;
        @SmartColumn(id = 4,name = "票价")
        private double price;
        @SmartColumn(id = 5,name = "取票状态")
        private String collect;

        public PInfo(String name, int sex, String ID, double price, boolean collect) {
            this.name = name;
            setSex(sex);
            this.ID = ID;
            this.price = price;
            setCollect(collect);
        }

        public void setCollect(boolean collect) {
            if (collect==true) {
                this.collect = "已取票";
            }else {
                this.collect = "未取票";
            }

        }

        public void setSex(int sex) {
            if (sex==0) {
                this.sex = "男";
            }else {
                this.sex = "女";
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }

        Bmob.initialize(this, "0802cbf346296dc9fac5a477f911ba39");

        final Fight fight = (Fight)getIntent().getSerializableExtra("FightInfo");
        allAccount=fight.getCurrentPeole();
        flightNum=fight.getFLIGHT();
        int year,month,day;
        year=fight.getDATE().getYear();
        month=fight.getDATE().getMonth();
        day=fight.getDATE().getDate();
        Date date=new Date(year,month,day-1,23,59,59);
        Date date1=new Date(year,month,day+1,0,0,0);
        up=date.getTime();
        down=date1.getTime();

        table = findViewById(R.id.table);
        bar=(NumberProgressBar)findViewById(R.id.number_progress_bar);
        load=(LinearLayout)findViewById(R.id.loading);
        info=(LinearLayout)findViewById(R.id.information);
        sta=(TextView)findViewById(R.id.sta);

        new Task().execute();
    }

    class Task extends AsyncTask<Void,Integer,Boolean>{
        @Override
        protected void onPreExecute() {
            info.setVisibility(View.GONE);
            load.setVisibility(View.VISIBLE);
        }

        @Override
        protected Boolean doInBackground(Void... voids) {
            String bq = "select * from Order where fight = '"+flightNum+"' "+
                    "and date > "+up+" "+
                    "and date < "+down;
            /*String bq = "select * from Order";*/
            /*Log.d("test", bq);*/
            new BmobQuery<Order>().doSQLQuery(bq,new SQLQueryListener<Order>(){
                @Override
                public void done(BmobQueryResult<Order> result, BmobException e) {
                    if(e ==null){
                        List<Order> list = (List<Order>) result.getResults();
                        if(list!=null && list.size()>0){
                            Plist.clear();
                            for (int i=0;i<list.size();i++){
                                p = new PInfo(list.get(i).getPassenger().getName(),
                                        list.get(i).getPassenger().getSex(),
                                        list.get(i).getPassenger().getIdentityCard(),
                                        list.get(i).getPrice(),
                                        list.get(i).isTicketCollect());
                                Plist.add(p);
                                finish=i;
                                income+=list.get(i).getPrice();
                            }
                            table.setData(Plist);
                            table.getConfig().setContentStyle(new FontStyle(50, Color.BLUE));
                            sta.setText("乘客数量："+allAccount+"人   累计收入："+income+"元");
                        }else{
                            ToastShow("暂时没有乘客购票");
                        }
                    }else{
                        Log.d("test", "error");
                        Log.d("test", "错误码："+e.getErrorCode()+"，错误描述："+e.getMessage());
                        ToastShow("错误码："+e.getErrorCode()+"，错误描述："+e.getMessage());
                    }
                }
            });
            return true;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            bar.setProgress(values[0]);
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            info.setVisibility(View.VISIBLE);
            load.setVisibility(View.GONE);
            ToastShow("已完成统计");
        }
    }

    public void ToastShow(String message){
        Toast.makeText(Detail.this,message,Toast.LENGTH_SHORT).show();
    }

}
